package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.recyclertype.ArticleComment;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.NumberFormatUtils;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 发现-评论
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/13 0013 9:55
 */
public class ArticleCommentProvider extends ItemViewProvider<ArticleComment, ArticleCommentProvider.ViewHolder> {

    private Activity mActivity;

    private OnLikeClickListener mListener;

    public ArticleCommentProvider(Activity activity, OnLikeClickListener listener) {
        mActivity = activity;
        mListener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_article_comment, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull ArticleComment articleComment) {

        StringBuilder builder = new StringBuilder();
        builder.append(articleComment.getAddTime());
        builder.append(" ");
        builder.append(articleComment.getAreaName());

        holder.listener = mListener;
        holder.articleComment = articleComment;

        holder.tvNickname.setText(articleComment.getDpName());
        holder.tvTimeAndAddress.setText(builder.toString());
        holder.tvContent.setText(articleComment.getContent());
        holder.tvLike.setText(NumberFormatUtils.getLargeFormatValue(articleComment.getVoteCount()));
        holder.ivLike.setSelected(articleComment.getHasVote() != 0);

        ImageLoader.loadAvatar(mActivity, holder.ivAvatar, articleComment.getSex());
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivAvatar;
        private TextView tvNickname;
        private ImageView ivLike;
        private TextView tvLike;
        private TextView tvTimeAndAddress;
        private TextView tvContent;

        public ArticleComment articleComment;

        public OnLikeClickListener listener;

        public ViewHolder(View itemView) {
            super(itemView);
            ivAvatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            tvNickname = (TextView) itemView.findViewById(R.id.tv_nickname);
            ivLike = (ImageView) itemView.findViewById(R.id.iv_like);
            tvLike = (TextView) itemView.findViewById(R.id.tv_like);
            tvTimeAndAddress = (TextView) itemView.findViewById(R.id.tv_time_and_address);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);

            ivLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onLikeClick(articleComment);
                    }
                }
            });
        }
    }

    public interface OnLikeClickListener {
        void onLikeClick(ArticleComment articleComment);
    }
}
